package com.briup.day35;

import java.io.File;
import java.util.Scanner;

/**
 * 文件检索*/
public class SearchDemo {
	
	static String searchName;
	static boolean match;
	static boolean casee;  //文件名大小写
	
	static int filecount;
	static int dircount;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入您要搜索的文件名：");
		searchName = sc.nextLine();
		System.out.println("是否要进行模糊匹配?Y/N");
		String s1 = sc.nextLine();
		System.out.println("文件名大小是否敏感?Y/N");
		String s2 = sc.nextLine();
		if ("Y".equals(s1.toUpperCase())) {
			match = true ;
		}
		if ("Y".equals(s2.toUpperCase())) {
			casee = true;
		}
			
		File dir = new File("src");
		
		long t1 = System.currentTimeMillis();
		searchFile(dir);
		long t2 = System.currentTimeMillis();
		System.out.println("搜索完成。共找到符合要求的文件" 
				+ filecount + "个，目录" + dircount + "个，"
				+ "共耗时" + (t2 - t1) + "毫秒。");
				
	}
	
	public static void searchFile(File file) {
		
		File[] files = null;
		
		if (file == null || (files = file.listFiles()) == null) {
			return ;
		}
		
		for (File file2 : files) {
			if (match) {
				if (casee) {
					if (file2.getName().contains(searchName)) {
						System.out.println(file2.getPath());
						if (file2.isFile())
							filecount++;
						else 
							dircount++;
					}
				}else {
					if (file2.getName().toUpperCase().
							contains(searchName.toUpperCase())) {
						System.out.println(file2.getPath());
						if (file2.isFile())
							filecount++;
						else 
							dircount++;
					}
				}

			}else {
				if (casee) {
					if(file2.getName().equals(searchName)) {
						System.out.println(file2.getPath());
						if (file2.isFile())
							filecount++;
						else 
							dircount++;
					}else {
						if(file2.getName().toUpperCase().
								equals(searchName.toUpperCase())) {
							System.out.println(file2.getPath());
							if (file2.isFile())
								filecount++;
							else 
								dircount++;
						}
					}
				}
			}
			searchFile(file2);
		}
		
	}

}
